import java.util.Stack;

public class Sort {

    // 插入排序
    public void insertSort(int[] array) {
        // write code  here
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 希尔排序
    public void shellSort(int[] array){
        // write code  here
        int gap = array.length/2;
        while(gap > 0){
            shell(array, gap);
            gap /= 2;
        }
    }

    private void shell(int[] array, int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0 ; j-=gap) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    // 选择排序
    public void selectSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[minIndex] > array[j]){
                    minIndex = j;
                }
            }
            swap(array, minIndex, i);
        }
    }

    //选择排序2  双向指针
    public void selectSort2(int[] array){
        // write code  here
        int left = 0;
        int right = array.length-1;

        while(left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if(array[minIndex] > array[i]){
                    minIndex = i;
                }
                if(array[maxIndex] < array[i]){
                    maxIndex = i;
                }
            }
            swap(array, left, minIndex);
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array, right, maxIndex);
            left++;
            right--;
        }
    }

    // 堆排序
    public void heapSort(int[] array){
        // write code  here
        buildBigHeap(array);
        int end = array.length-1;
        for (int i = 0; i < array.length-1; i++) {
            swap(array, 0, end);
            shifDown(array, 0, end);
            end--;
        }
    }

    //建立一个大根堆
    private void buildBigHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            shifDown(array, parent, array.length);
        }
    }

    //堆排向下调整
    private void shifDown(int[] array, int parent, int len){
        int child = (2*parent)+1;

        while(child < len){
            if(child+1 < len && array[child] < array[child+1]){
                child++;
            }

            if(array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = (2 * parent) + 1;
            }else{
                break;
            }
        }
    }

    // 冒泡排序
    public void bubbleSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = i+1; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j, j+1);
                    flg = true;
                }
            }
            if(flg == false){
                break;
            }
        }
    }

    // 快速排序
    public void quickSort(int[] array){
        // write code  here
        //quick2(array, 0, array.length-1);
        quick(array,0,array.length-1);
    }

    //快排用的插入排序
    public void selectSort(int[] array, int start, int end){
        for (int i = start+1; i <= end; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= start ; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private int findMidIndex(int[] array, int start, int end){
        int mid = (start+end)/2;

        if(array[start] < array[end]){
            if(array[mid] > array[end]){
                return end;
            } else if (array[mid] < array[start]) {
                return start;
            }else{
                return mid;
            }
        }else{
            if(array[mid] < array[end]){
                return end;
            } else if (array[mid] > array[start]) {
                return start;
            }else{
                return mid;
            }
        }
    }
    private void quick(int[] array, int start, int end){
        if(start >= end){
            return;
        }

        //优化，当递归到最后几层的时候，数据以及趋于有序，直接使用插入排序
        if(end - start > 10){
            selectSort(array, start, end);
        }

        //优化，找到第一个，中间，最后一个中排中间的数与第一个交换
        int ret = findMidIndex(array, start, end);
        swap(array, ret, start);

        int pivot = partition(array, start, end);
        quick(array, start, pivot-1);
        quick(array, pivot+1,end);
    }

    //自己写的递归快排
    private void quick2(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        int i = left;
        int j = right;
        int pivot = array[left];

        while(left < right){
            while(left < right && array[right] >= pivot){
                right--;
            }
            while(left < right && array[left] <= pivot){
                left++;
            }
            if(left < right){
                swap(array,left,right);
                left++;
                right--;
            }
        }
        swap(array, i, left);

        quick2(array, i, right-1);
        quick2(array, right+1, j);
    }

    //hoare快排
    private int partitionHoare(int[] array, int left, int right){
        int tmp = array[left];
        int i = left;
        while(left < right){
            while(left<right && array[right] >= tmp){
                right--;
            }
            while(left<right && array[left] <= tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }

    //挖坑快排
    private int partition(int[] array, int left, int right){
        int tmp = array[left];

        while(left < right){
            while(left < right && array[right] >= tmp){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //非递归实现快排
    public void quickSort2(int[] array){
        int left = 0;
        int right = array.length-1;
        Stack<Integer> stack = new Stack<>();
        stack.push(left);
        stack.push(right);

        while(!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();

            int pivot = partition(array,left,right);
            if(pivot-1 > left){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1 < right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    //归并排序
    public void mergeSort(int[] array){
        mergeSortChild(array, 0, array.length-1);
    }

    private void mergeSortChild(int[] array, int left, int right){
        if(left >= right){
            return;
        }

        int mid = (left+right)/2;
        mergeSortChild(array, left ,mid);
        mergeSortChild(array,mid+1, right);
        merge(array, left, mid, right);
    }

    //归并
    private void merge(int[] array, int left, int mid, int right){
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        while(s1 <= e1 && s2 <= e2){
            if(array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else{
                tmp[k++] = array[s2++];
            }
        }

        while(s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while(s2 <= e2){
            tmp[k++] = array[s2++];
        }

        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    public void mergeSort2(int[] array){
        int gap = 1;
        while(gap < array.length){
            for (int i = 0; i < array.length; i += gap*2) {
                int left = i;
                int mid = left+gap-1;
                int right = left+gap*2-1;
                if(mid >= array.length){
                    mid = array.length-1;
                }
                if(right >= array.length){
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    //交换函数
    private void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

}
